package com.zw.pdm.module.craft.service;

import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.production.*;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.craft.mapper.CraftMaintainMapper;
import com.zw.pdm.module.craft.mapper.CraftReceiveMapper;
import com.zw.pdm.module.craft.model.CarMakeResultModel;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CraftMaintainService {

    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private CraftMakeDetailRepository craftMakeDetailRepository;
    @Autowired
    private CraftReceiveMapper craftReceiveMapper;
    @Autowired
    private ChangeMapper changeMapper;
    @Autowired
    private CraftMaintainMapper craftMaintainMapper;
    @Autowired
    private LogService logService;

    /**
     * 接收列表查询cra_make_sheet
     */
    public List<CraMakeSheet> selectCraftMakeSheet(CraMakeSheet craMakeSheet) {
        return craftMaintainMapper.selectCraftMakeSheet(craMakeSheet);
    }

    public CraMakeDetail selectCraftMakeDetailOld(Integer id) {
        return craftMaintainMapper.selectCraftMakeDetailOld(id);
    }

    /**
     * 查询明细表根据主表ID cra_make_detail
     *
     * @param sheetId
     * @return
     */
    public List<CraMakeDetail> selectCraftMakeDetail(Integer sheetId) {
        return craftMaintainMapper.selectCraftMakeDetail(sheetId);
    }

    /**
     * 通过ID查询makesheet
     *
     * @param id
     * @return
     */
    public CraMakeSheet selectCraftMakeSheetModel(Integer id) {
        return craftMaintainMapper.selectCraftMakeSheetModel(id);
    }

    /**
     * 拆分插入数据
     *
     * @param craMakeDetail
     * @param craMakeDetailNew
     * @return
     */
    public boolean insertMaintainDetail(CraMakeDetail craMakeDetail, CraMakeDetail craMakeDetailNew) {
        Integer orderNum = craMakeDetail.getOrderNum();
        Integer pid = 0;
        try {
            pid = craftMaintainMapper.selectMaxOrderNum(craMakeDetail.getId());
        } catch (Exception ex) {

        }

        craMakeDetail.setResultId(null);
        craMakeDetail.setId(null);
        craMakeDetail.setDetailPid(craMakeDetailNew.getId());
        craMakeDetail.setDrawNo(craMakeDetailNew.getDrawNo());
        craMakeDetail.setDrawName(craMakeDetailNew.getDrawName());
        craMakeDetail.setDrawNum(craMakeDetailNew.getDrawNum());
        craMakeDetail.setRemark(craMakeDetailNew.getRemark());
        craMakeDetail.setQuality(craMakeDetailNew.getQuality());
        craMakeDetail.setOrderNum(orderNum);
        if (craMakeDetailNew.getMakeType().equals("装配件")) {
            craMakeDetail.setIsNeedCard(0);
        } else {
            craMakeDetail.setIsNeedCard(1);
        }
        craMakeDetail.setMakeType(craMakeDetailNew.getMakeType());
        craMakeDetail.setPid(pid + 1);
        craMakeDetail.setCreatTime(new Date());
        craMakeDetail.setLastTime(new Date());
        craMakeDetail.setCreatName(ShiroUtils.getUser().getName());
        craMakeDetail.setChangeStatus(0);
        craftMakeDetailRepository.save(craMakeDetail);
        logService.saveLog("工艺制作件清单拆分", "工艺制作件清单明细表拆分ID：" + craMakeDetail.getId());
        return true;
    }

    /**
     * 拆分修改数据
     *
     * @param craMakeDetail
     * @param craMakeDetailNew
     * @return
     */
    public boolean editMaintainDetail(CraMakeDetail craMakeDetail, CraMakeDetail craMakeDetailNew) {
//        List<Card> cardList = craftMaintainMapper.selectCardByCraMakeDetailId(craMakeDetail.getId());
//        if (cardList.size() > 0) {
//            return false;
//        }
        craMakeDetail.setDrawNo(craMakeDetailNew.getDrawNo());
        craMakeDetail.setDrawName(craMakeDetailNew.getDrawName());
        craMakeDetail.setDrawNum(craMakeDetailNew.getDrawNum());
        craMakeDetail.setRemark(craMakeDetailNew.getRemark());
        if (craMakeDetailNew.getMakeType().equals("装配件")) {
            craMakeDetail.setIsNeedCard(0);
        } else {
            craMakeDetail.setIsNeedCard(1);
        }
        craMakeDetail.setMakeType(craMakeDetailNew.getMakeType());
        craMakeDetail.setLastTime(new Date());
        craMakeDetail.setCreatName(ShiroUtils.getUser().getName());
        List<Card> cardList = craftMaintainMapper.selectCardByCraMakeDetailId(craMakeDetail.getId());
        //判断  拆分的图号，如果有工艺卡，做变更提醒，如果没有工艺卡直接修改成功不做提醒-2020.12.7-kz
        if (cardList.size() > 0) {
            craMakeDetail.setChangeStatus(0);
            craMakeDetail.setChangeType(1);
            craMakeDetail.setTechnologyChange(1);
        }
        craftMakeDetailRepository.save(craMakeDetail);
        logService.saveLog("工艺制作件清单拆分修改", "工艺制作件清单明细表拆分ID：" + craMakeDetail.getId());
        return true;
    }

    /**
     * 删除明细表数据根据ID
     *
     * @param id
     * @return
     */
    public boolean delCraMakeDetailRelease(Integer id) {
        List<Card> cardList = craftMaintainMapper.selectCardByCraMakeDetailId(id);
        if (cardList.size() > 0) {
            return false;
        }
        craftMaintainMapper.delCraMakeDetailRelease(id);
        logService.saveLog("删除工艺制作件清单拆分", "工艺制作件清单明细表ID：" + id);
        return true;
    }

    /**
     * @param id            修改ID
     * @param isNeedCard    是否需要工艺卡
     * @param outsourcing   是否整体外协
     * @param isOutsourcing 整体外协
     * @return
     */
    public boolean outsourcingCraMakeDetailRelease(Integer id, Integer isNeedCard, Integer outsourcing, Integer isOutsourcing) {
        List<Card> cardList = craftMaintainMapper.selectCardByCraMakeDetailId(id);
        CraMakeDetail craMakeDetail = craftMakeDetailRepository.getOne(id);
        if (cardList.size() > 0 && isOutsourcing == 1) {
            return false;
        }
        craMakeDetail.setId(id);
        if (isOutsourcing == 0) {
            if (craMakeDetail.getMakeType() == "装配件") {
                craMakeDetail.setIsNeedCard(0);
            } else {
                craMakeDetail.setIsNeedCard(isNeedCard);
            }
        } else {

            craMakeDetail.setIsNeedCard(isNeedCard);
        }
        craMakeDetail.setOutsourcing(outsourcing);
        craftMaintainMapper.outsourcingCraMakeDetailRelease(craMakeDetail);
        logService.saveLog("整体外协", "工艺制作件清单明细表ID：" + id);
        return true;
    }
}
